package com.msblearn.C_005;

import java.util.concurrent.TimeUnit;

/**
 * T10_ReentrantLock:可重入锁
 * Synchronized就是可重入锁。
 */
public class T01_ReentrantLock1 {

    synchronized void m1() {
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
            if (i == 2) {
                m2();
            }
        }
    }

    synchronized void m2() {
        System.out.println("m2.....");
    }

    public static void main(String[] args) throws InterruptedException {
        T01_ReentrantLock1 reentrantLock1 = new T01_ReentrantLock1();
        new Thread(reentrantLock1::m1).start();
        TimeUnit.SECONDS.sleep(1);
        //会等待m1完成才会执行。等待锁。
        new Thread(reentrantLock1::m2).start();

    }
}
